var formElementError = 'form_element_error'; // red border

var errorState = 'ui-state-error ui-corner-all'; // round corners, red background
var successState = 'ui-state-highlight ui-corner-all'; // round corners, yellow background

var alertIcon = 'ui-icon ui-icon-alert'; // alert icon
var alertSpan = '<span class="'+ alertIcon +'" style="float: left; margin: 3px; margin-right: .3em;"></span>';

var defaultMsgDiv = 'msg_div';
var defaultErr = 'Error occured while processing request. Please contact the administrator.';

$.extend({
	Msgs: function(ndx, a, b) {
		var msgs = new Array();
		a = a || 'This field';
		b = b || '';
		
		msgs['required'] = a + ' is required.';
		msgs['unique'] = a + ' must contain a unique value.';
		msgs['match'] = a + ' does not match ' + b + '.';
		msgs['validDate'] = a + ' must contain a valid date.';
		msgs['dateFormat'] = a + ' must be in ' + b + ' format';
		msgs['validEmail'] = a + ' must contain a valid email address.';
		msgs['minLength'] = a + ' must be at least ' + b + ' characters in length.';
		msgs['maxLength'] = a + ' cannot exceed ' + b + ' characters in length.';
		msgs['exactLength'] = a + ' must be exactly ' + b + ' characters in length.';
		msgs['max'] = a + ' cannot be more than ' + b + '.';
		msgs['min'] = a + ' cannot be less than ' + b + '.';
		msgs['integer'] = a + ' must be an integer.';
		msgs['decimal'] = a + ' must contain a decimal number.';
		msgs['image'] = a + ' must be an image.';
		msgs['afterDate'] = a + ' must be after ' + b + '.';
		msgs['validUrl'] = a + ' must contain a valid URL.';
		
		return msgs[ndx];
	},
	
	/**
	 * Generic dialog boxes
	 */
	genericDialog: function(div, id, callback) {
		id = id || '';
		
		$('#' + div).dialog({
			resizable: false,
			height: 'auto',			
			modal: true,
			buttons: {
				'No': function() {
					$(this).dialog('close');
				},
				'Yes': function() {
					eval(callback + '("' + id + '")');
					$(this).dialog('close');
				}
			},
			close: function() {
				$(this).dialog('destroy');
				$('#' + div).addClass('hidden');
				
			}
		}).removeClass('hidden');
	},
	
	confirmDelete: function(id, callback, title, div, msg) {
		div = div || 'ddelete';
		title = title || '';
  
		if (msg) $('#' + div+ ' > span:not([class="icon icon-info"])').text(msg);
		
		$('#' + div).attr('title', title);
		$.genericDialog(div, id, callback);
	},
	confirmVoid: function(id, callback, title, div) {
		$('#sdelete').text('void');
		$.confirmDelete(id, callback, title, div);
	},
	showError: function(msg, msgDiv) {
		msgDiv = msgDiv || defaultMsgDiv;

		msg = msg || defaultErr;

		$('#' + msgDiv).removeClass(successState);
		$('#' + msgDiv).addClass(errorState);
		$('#' + msgDiv).html(alertSpan + msg);

		$('#' + msgDiv).fadeIn('slow');
	},
	appendError: function(error, element, div) {
		error.append('<br />');
		error.attr('id', 'error_' + element.attr('name'));
		error.appendTo(div);

		// dirty hack to align error list vertically
		div.children('.error').removeClass('error_3');
		div.children('.error').slice(1).each(function() {
			$(this).addClass('error_3');
		});
	},
	highlightFormElement: function(element, div) {
		var name = $(element).attr('name');

		// remove messages associated with element
		if ($('#error_'+name).length)
		{
			$('#error_'+name).remove();
		}

		if ($(div + ':hidden').length) $(div + ':hidden').fadeIn('slow'); // show if hidden
		$(element).addClass(formElementError);

		$('#msg_div').fadeOut('slow');
	},
	unhighlightFormElement: function(element, div) {
		var name = $(element).attr('name');

		// dirty hack to align error list vertically
		if (div)
		{
			div.children('.error').removeClass('error_3');
			div.children('.error').slice(1).each(function() {
				$(this).addClass('error_3');
			});
		}

		// remove messages associated with element
		if ($('#error_'+ name).length)
		{
			$('#error_'+ name).remove();

			if ( ! $('.error').length) div.fadeOut('slow'); // hide error div
		}

                $(element).removeClass(formElementError);
	},
	unhighlightAll: function(objForm, div) {
		objForm.find('.' + formElementError).each(function() {
			$(this).removeClass(formElementError);
			var name = $(this).attr('name');

			if ($('#error_'+ name).length) {
				$('#error_'+ name).remove();
			}
		});

		if ( ! $('.error').length) div.fadeOut('slow'); // hide error div
	},
	loading: function(title) {
		$('#dloading').removeClass('hidden');
		$('#dfinished').addClass('hidden');
		
		$('#dprocessing').attr('title', title).dialog({
			resizable: false,
			closeOnEscape: false,
			height: 170,
			modal: true,
			open: function() {
				$('a.ui-dialog-titlebar-close').addClass('hidden');
			},
			close: function() {
				$(this).dialog('destroy');
				$('#dprocessing').addClass('hidden');
			}
		}).removeClass('hidden');
	},
	
	finished: function(html, iconClass, callback) {
		callback = callback || null;
		
		$('#dprocessing').dialog('option', 'buttons', {
			'Close': function() {
				if (callback != null) eval(callback);
				$(this).dialog('close');
			}
		});
			
		$('#dloading').addClass('hidden');
		$('#ficon').addClass(iconClass);
		$('#fmsg').html(html);
		$('#dfinished').removeClass('hidden');
	}
});

//extends qtip styles

$.fn.qtip.styles.fielderror = {
	color: '#cd0a0a',
	textAlign: 'center',
	border: {
		width: 1,
		radius: 3,
		color: '#aaa'
	},
	tip: 'leftBottom',
	name: 'light'
};

$.fn.qtip.styles.Rfielderror = {
	color: '#cd0a0a',
	textAlign: 'center',
	border: {
		width: 1,
		radius: 3,
		color: '#aaa'
	},
	tip: 'rightBottom',
	name: 'light'
};

$.fn.qtip.styles.fieldinfo = {
	color: '#333',
	textAlign: 'center',
	border: {
		width: 1,
		radius: 3,
		color: '#aaa'
	},
	tip: 'bottomMiddle',
	name: 'light'
};

$.fn.qtip.styles.tdinfo = {
	color: '#333',
	textAlign: 'left',
	border: {
		width: 1,
		radius: 3,
		color: '#aaa'
	},
	tip: 'leftBottom',
	name: 'light'
};

$.fn.qtip.styles.filtererror = {
	color: '#cd0a0a',
	textAlign: 'center',
	border: {
		width: 1,
		radius: 3,
		color: '#aaa'
	},
	tip: 'bottomMiddle',
	name: 'light'
};
